//
//  ZegoUIKitPrebuiltCallWaitingVC.swift
//  ZegoUIKit
//
//  Created by zego on 2022/8/11.
//

import UIKit
import Kingfisher

class ZegoUIKitPrebuiltCallWaitingVC: UIViewController {
    
    private let help: ZegoUIKitPrebuiltCallWaitingVC_Help = ZegoUIKitPrebuiltCallWaitingVC_Help()
    
    @IBOutlet weak var backgroundImage: UIImageView! {
        didSet {
            backgroundImage.image = ZegoUIKitCallIconSetType.call_waiting_bg.load()
        }
    }
    
    @IBOutlet weak var videoPreviewView: ZegoAudioVideoView! {
        didSet {
            if self.isInviter && self.callInvitationData?.type == .videoCall {
                videoPreviewView.userID = self.callInvitationData?.inviter?.userID
            }
        }
    }
    
    @IBOutlet weak var headLabel: UILabel! {
        didSet {
            headLabel.layer.masksToBounds = true
            headLabel.layer.cornerRadius = 50
            if !self.isInviter {
                self.setHeadUserName(callInvitationData?.inviter?.userName)
            } else {
                
            }
        }
    }
    
    @IBOutlet weak var userNameLabel: UILabel! {
        didSet {
            userNameLabel.text = callInvitationData?.inviter?.userName
            userNameLabel.isHidden = true
        }
    }
    @IBOutlet weak var callStatusLabel: UILabel!
    // 开关摄像头
    private lazy var switchCameraButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage("ic_camera_nor".toImage, for: .normal)
        button.setImage("ic_camera_off".toImage, for: .selected)
        button.addTarget(self, action: #selector(switchCameraAction), for: .touchUpInside)
        return button
    }()
    
    @IBOutlet weak var declineView: UIView! {
        didSet {
            declineView.isHidden = self.isInviter
        }
    }
    @IBOutlet weak var acceptView: UIView! {
        didSet {
            acceptView.isHidden = self.isInviter
        }
    }
    
    @IBOutlet weak var declineButton: ZegoRefuseInvitationButton! {
        didSet {
            declineButton.delegate = self.help
            if let inviter = callInvitationData?.inviter {
                declineButton.inviterID = inviter.userID
            }
        }
    }
    
    // 开关摄像头
    @objc func switchCameraAction(_ sender: UIButton) {
        
        sender.isSelected = !sender.isSelected
        xLog("sender isSelected \(sender.isSelected) \(sender.currentTitle ?? "")")
        if sender.isSelected {
            ZegoUIKitPrebuiltCallProfile.shared.isSwitchCamera = false
            ZegoExpressEngine.shared().stopPreview()
            
        } else {
            ZegoUIKitPrebuiltCallProfile.shared.isSwitchCamera = true
            let canvas = ZegoCanvas(view: videoPreviewView)
            canvas.viewMode = ZegoViewMode(rawValue: videoPreviewView.videoFillMode.rawValue) ?? .aspectFill
            ZegoExpressEngine.shared().startPreview(canvas)
            
        }
    }
    
    @IBOutlet weak var declineButtonLabel: UILabel!
    
    @IBOutlet weak var cancelInviationButton: ZegoCancelInvitationButton! {
        didSet {
            cancelInviationButton.delegate = self.help
            cancelInviationButton.isHidden = !self.isInviter
            if let invitees = callInvitationData?.invitees {
                for user in invitees {
                    guard let userID = user.userID else { return }
                    cancelInviationButton.invitees.append(userID)
                }
            }
            
        }
    }
    
    @IBOutlet weak var acceptButton: UIButton!
    //    @IBOutlet weak var acceptButton: ZegoAcceptInvitationButton! {
    //        didSet {
    //            acceptButton.delegate = self.help
    //            if let inviter = callInvitationData?.inviter {
    //                acceptButton.inviterID = inviter.userID
    //            }
    //        }
    //    }
    
    @IBOutlet weak var acceptButtonLabel: UILabel!
    
    @IBOutlet weak var switchFacingCameraButton: ZegoSwitchCameraButton! {
        didSet {
            switchFacingCameraButton.iconBackFacingCamera = ZegoUIKitCallIconSetType.icon_camera_overturn.load()
            switchFacingCameraButton.iconFrontFacingCamera = ZegoUIKitCallIconSetType.icon_camera_overturn.load()
        }
    }
    
    @IBOutlet weak var userAvatarView: UIImageView!
    // 邀请通话人的信息
    var callInvitationData: ZegoCallInvitationData? {
        didSet {
            let config = ZegoUIKitPrebuiltCallInvitationService.shared.config
            var userNameTextFormat = "%@"
            xLog("callInvitationData?.customData \(callInvitationData?.customData ?? "空")")
            
            if !self.isInviter {
                // 被邀请
                if callInvitationData?.type == .videoCall {
                    userNameTextFormat = callInvitationData?.invitees?.count ?? 0 > 1 ? config?.translationText.incomingGroupVideoCallPageTitle ?? "%@" : config?.translationText.incomingVideoCallPageTitle ?? "%@"
                } else {
                    userNameTextFormat = callInvitationData?.invitees?.count ?? 0 > 1 ? config?.translationText.incomingGroupVoiceCallPageTitle ?? "@" : config?.translationText.incomingVoiceCallPageTitle ?? "%@"
                }
                self.userNameLabel.text = String(format: userNameTextFormat , callInvitationData?.inviter?.userName ?? "")
                self.setHeadUserName(callInvitationData?.inviter?.userName)
                self.callStatusLabel.text = callInvitationData?.invitees?.count ?? 0 > 1 ? (callInvitationData?.type == .videoCall ? config?.translationText.incomingGroupVideoCallPageMessage : config?.translationText.incomingGroupVoiceCallPageMessage) : (callInvitationData?.type == .videoCall ? config?.translationText.incomingVideoCallPageMessage : config?.translationText.incomingVoiceCallPageMessage)
                
            } else {
                //self.userNameLabel.text = callInvitationData?.invitees?.first?.userName
                userNameTextFormat = callInvitationData?.type == .videoCall ? config?.translationText.outgoingVideoCallPageTitle ?? "%@" : config?.translationText.outgoingVoiceCallPageTitle ?? "%@"
                self.userNameLabel.text = String(format: userNameTextFormat, callInvitationData?.invitees?.first?.userName ?? "")
                self.setHeadUserName(callInvitationData?.invitees?.first?.userName)
                self.callStatusLabel.text = callInvitationData?.type == .videoCall ? config?.translationText.outgoingVideoCallPageMessage : config?.translationText.outgoingVoiceCallPageMessage
            }
            
            if let invitees = callInvitationData?.invitees {
                for user in invitees {
                    guard let userID = user.userID else { return }
                    self.cancelInviationButton.invitees.append(userID)
                }
            }
            
            let refuseData: [String : AnyObject] = [
                "reason": "decline" as AnyObject,
                "invitationID": callInvitationData?.invitationID as AnyObject
            ]
            declineButton.data = refuseData.jsonString
            
            if let inviter = callInvitationData?.inviter {
                declineButton.inviterID = inviter.userID
                // acceptButton.inviterID = inviter.userID
                
                if callInvitationData?.type == .videoCall {
                    // acceptButton.icon = ZegoUIKitCallIconSetType.call_video_icon.load()
                    acceptButton.setImage(ZegoUIKitCallIconSetType.call_video_icon.load(), for: .normal)
                } else {
                    // acceptButton.icon = ZegoUIKitCallIconSetType.call_accept_icon.load()
                    acceptButton.setImage(ZegoUIKitCallIconSetType.call_accept_icon.load(), for: .normal)
                }
            }
            if self.isInviter && callInvitationData?.type == .videoCall {
                if let videoConfig = ZegoUIKitPrebuiltCallInvitationService.shared.config?.videoConfig {
                    ZegoUIKit.shared.setVideoConfig(config: videoConfig.resolution)
                }
                videoPreviewView.userID = callInvitationData?.inviter?.userID
                videoPreviewView.isHidden = false
                switchFacingCameraButton.isHidden = false
            } else {
                videoPreviewView.isHidden = true
                switchFacingCameraButton.isHidden = true
            }
            self.acceptButtonLabel.text = config?.translationText.incomingCallPageAcceptButton
            self.declineButtonLabel.text = config?.translationText.incomingCallPageDeclineButton
            
            
            if !isInviter {
                // 被邀请， 显示对方的头像
                if let inviter = callInvitationData?.inviter {
                    let userAvatar: String = inviter.userAvatar ?? ""
                    if userAvatar != "" {
                        self.userAvatarView.isHidden = false
                        self.headLabel.isHidden = true
                        self.userAvatarView.kf.setImage(with: URL(string: userAvatar))
                        videoPreviewView.avatarUrl = userAvatar
                    }
                }
            } else {
                // 邀请对方通话，显示对方的头像
                if let inviter = callInvitationData?.invitees?.first {
                    let userAvatar: String = inviter.userAvatar ?? ""
                    if userAvatar != "" {
                        self.userAvatarView.isHidden = false
                        self.headLabel.isHidden = true
                        self.userAvatarView.kf.setImage(with: URL(string: userAvatar))
                        videoPreviewView.avatarUrl = userAvatar
                    }
                }
            }
            
            
            
            //            if let inviter = callInvitationData?.inviter {
            //                let userAvatar: String = (ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.onUserIDUpdated?(user: inviter) ?? "") as String
            //                xLog("[IM] 用户头像 [\(userAvatar)]")
            //                if userAvatar != "" {
            //                    loadImage(imageUrl: (userAvatar))
            //                    videoPreviewView.avatarUrl = userAvatar
            //                }
            //            }
            
        }
    }
    
    
    /// true: 邀请，false：被邀请
    var isInviter: Bool = false {
        didSet {
            if isInviter {
                cancelInviationButton.isHidden = false
                acceptView.isHidden = true
                declineView.isHidden = true
                switchCameraButton.isHidden = false
                
            } else {
                cancelInviationButton.isHidden = true
                acceptView.isHidden = false
                declineView.isHidden = false
                switchCameraButton.isHidden = true
            }
        }
    }
    
    var showDeclineButton: Bool = true {
        didSet {
            if showDeclineButton == false {
                self.declineView.isHidden = true
                let acceptRect: CGRect = self.acceptView.frame
                let x: CGFloat = (self.view.frame.width - acceptRect.width)/2
                self.trailingConstraint.constant = x
            }
        }
    }
    
    
    @IBOutlet weak var trailingConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        ZegoUIKit.shared.addEventHandler(self.help)
        self.help.waitingVC = self
        
        
        view.addSubview(switchCameraButton)
        switchCameraButton.snp.makeConstraints { make in
            make.centerY.equalTo(cancelInviationButton)
            make.left.equalToSuperview().offset(70)
            make.size.equalTo(CGSizeMake(60, 60))
        }
        
        acceptButton.addTarget(self, action: #selector(onAcceptInvitationAction), for: .touchUpInside)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIApplication.sg.isIdleTimerDisabled(true)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        UIApplication.sg.isIdleTimerDisabled(false)
    }
    
    private func setHeadUserName(_ userName: String?) {
        guard let userName = userName else { return }
        if userName.count > 0 {
            let firstStr: String = String(userName[userName.startIndex])
            self.headLabel.text = firstStr
        }
    }
    
}

class ZegoUIKitPrebuiltCallWaitingVC_Help: NSObject, ZegoAcceptInvitationButtonDelegate, ZegoCancelInvitationButtonDelegate, ZegoRefuseInvitationButtonDelegate, ZegoUIKitEventHandle {
    
    weak var waitingVC: ZegoUIKitPrebuiltCallWaitingVC?
    
    func onRefuseInvitationButtonClick() {
        ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.onIncomingCallDeclineButtonPressed?()
        ZegoUIKitPrebuiltCallInvitationService.shared.invitationData = nil
        ZegoUIKitPrebuiltCallInvitationService.shared.callID = nil
        ZegoCallAudioPlayerTool.stopPlay()
        waitingVC?.dismiss(animated: true, completion: nil)
    }
    
    // 接受邀请
    func onAcceptInvitationButtonClick() {
        ZegoCallAudioPlayerTool.stopPlay()
        guard let callInvitationData = self.waitingVC?.callInvitationData else { return }
        waitingVC?.dismiss(animated: false, completion: {
            var normalConfig = ZegoUIKitPrebuiltCallConfig.oneOnOneVideoCall()
            if callInvitationData.invitees?.count ?? 0 > 1 {
                //group call
                normalConfig = callInvitationData.type == .videoCall ? ZegoUIKitPrebuiltCallConfig.groupVideoCall() : ZegoUIKitPrebuiltCallConfig.groupVoiceCall()
            } else {
                //one on one call
                normalConfig =  callInvitationData.type == .videoCall ? ZegoUIKitPrebuiltCallConfig.oneOnOneVideoCall() :  ZegoUIKitPrebuiltCallConfig.oneOnOneVoiceCall()
            }
            xLog("[IM] 同意接受邀请")
            xLog("callid : \(callInvitationData.callID ?? "kong")")
            xLog("邀请人的信息 id : \(callInvitationData.inviter?.userID ?? "kong")")
            xLog("邀请人的信息 userName : \(callInvitationData.inviter?.userName ?? "kong")")
            
            let config: ZegoUIKitPrebuiltCallConfig = ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.requireConfig(callInvitationData) ?? normalConfig
            let callVC: ZegoUIKitPrebuiltCallVC = ZegoUIKitPrebuiltCallVC.init(callInvitationData, config: config)
            callVC.modalPresentationStyle = .fullScreen
            callVC.delegate = ZegoUIKitPrebuiltCallInvitationService.shared.help
            currentViewController()?.present(callVC, animated: false, completion: nil)
            ZegoUIKitPrebuiltCallInvitationService.shared.callVC = callVC
            ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.onIncomingCallAcceptButtonPressed?()
        })
    }
    
    func onCancelInvitationButtonClick() {
        ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.onOutgoingCallCancelButtonPressed?()
        ZegoUIKitPrebuiltCallInvitationService.shared.invitationData = nil
        ZegoUIKitPrebuiltCallInvitationService.shared.callID = nil
        ZegoCallAudioPlayerTool.stopPlay()
        waitingVC?.dismiss(animated: true, completion: nil)
    }
    
    // 邀请被接受
    func onInvitationAccepted(_ invitee: ZegoUIKitUser, data: String?) {
        if !ZegoUIKitPrebuiltCallInvitationService.shared.isGroupCall {
            guard let callInvitationData = self.waitingVC?.callInvitationData else { return }
            waitingVC?.dismiss(animated: false, completion: {
                ZegoCallAudioPlayerTool.stopPlay()
                var normalConfig = ZegoUIKitPrebuiltCallConfig.oneOnOneVideoCall()
                if callInvitationData.invitees?.count ?? 0 > 1 {
                    //group call
                    normalConfig = callInvitationData.type == .videoCall ? ZegoUIKitPrebuiltCallConfig.groupVideoCall() : ZegoUIKitPrebuiltCallConfig.groupVoiceCall()
                } else {
                    //one on one call
                    normalConfig =  callInvitationData.type == .videoCall ? ZegoUIKitPrebuiltCallConfig.oneOnOneVideoCall() :  ZegoUIKitPrebuiltCallConfig.oneOnOneVoiceCall()
                }
                let config: ZegoUIKitPrebuiltCallConfig = ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.requireConfig(callInvitationData) ?? normalConfig
                
                let callVC: ZegoUIKitPrebuiltCallVC = ZegoUIKitPrebuiltCallVC.init(callInvitationData, config: config)
                callVC.modalPresentationStyle = .fullScreen
                callVC.delegate = ZegoUIKitPrebuiltCallInvitationService.shared.help
                currentViewController()?.present(callVC, animated: false, completion: nil)
                ZegoUIKitPrebuiltCallInvitationService.shared.callVC = callVC
                let callee = self.getCallUser(invitee)
                let callID: String? = ZegoUIKitPrebuiltCallInvitationService.shared.invitationData?.callID
                ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.onOutgoingCallAccepted?(callID ?? "", callee: callee)
                ZegoUIKitPrebuiltCallInvitationService.shared.state = .accept
            })
        }
    }
    
    func onInvitationCanceled(_ inviter: ZegoUIKitUser, data: String?) {
        if self.waitingVC?.callInvitationData?.inviter?.userID == inviter.userID {
            self.waitingVC?.dismiss(animated: true, completion: nil)
            ZegoUIKitPrebuiltCallInvitationService.shared.invitationData = nil
            ZegoUIKitPrebuiltCallInvitationService.shared.callID = nil
        }
    }
    
    func onInvitationRefused(_ invitee: ZegoUIKitUser, data: String?) {
        if !ZegoUIKitPrebuiltCallInvitationService.shared.isGroupCall {
            let callee = getCallUser(invitee)
            let callID: String? = ZegoUIKitPrebuiltCallInvitationService.shared.invitationData?.callID
            let callData: [String: AnyObject]? = data?.convertStringToDictionary()
            if let callData = callData, callData["reason"] as? String ?? "" == "busy" {
                ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.onOutgoingCallRejectedCauseBusy?(callID ?? "", callee: callee)
            } else {
                ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.onOutgoingCallDeclined?(callID ?? "", callee: callee)
            }
            
            if let invitationInvitees = ZegoUIKitPrebuiltCallInvitationService.shared.invitees
            {
                for invitationUser in invitationInvitees {
                    if invitee.userID == invitationUser.user?.userID {
                        invitationUser.state = .refuse
                    }
                }
            }
            self.waitingVC?.dismiss(animated: true, completion: nil)
            ZegoUIKitPrebuiltCallInvitationService.shared.invitationData = nil
            ZegoUIKitPrebuiltCallInvitationService.shared.callID = nil
            ZegoCallAudioPlayerTool.stopPlay()
        }
    }
    
    func onInvitationTimeout(_ inviter: ZegoUIKitUser, data: String?) {
        if inviter.userID == self.waitingVC?.callInvitationData?.inviter?.userID {
            self.waitingVC?.dismiss(animated: true, completion: nil)
            ZegoUIKitPrebuiltCallInvitationService.shared.invitationData = nil
            ZegoUIKitPrebuiltCallInvitationService.shared.callID = nil
            ZegoCallAudioPlayerTool.stopPlay()
        }
    }
    
    func onInvitationResponseTimeout(_ invitees: [ZegoUIKitUser], data: String?) {
        let curInvitee = self.waitingVC?.callInvitationData?.invitees?.first
        let timeoutInvitee = invitees.first
        if curInvitee?.userID == timeoutInvitee?.userID {
            self.waitingVC?.dismiss(animated: true, completion: nil)
            ZegoUIKitPrebuiltCallInvitationService.shared.invitationData = nil
            ZegoUIKitPrebuiltCallInvitationService.shared.callID = nil
        }
    }
    
    func getCallUser(_ user: ZegoUIKitUser) -> ZegoCallUser {
        let callUser: ZegoCallUser = ZegoCallUser()
        callUser.id = user.userID
        callUser.name = user.userName
        return callUser
    }
}

extension ZegoUIKitPrebuiltCallWaitingVC {
    
    @objc func onAcceptInvitationAction() {
        xLog("同意邀请视频通话")
        guard let callInvitationData = callInvitationData else { return }
        if UserProfile.country > 0 {
            // 我是俄罗斯的,不需要判断积分和身份
            receivedInvitation()
            return
        }
        
        guard let inviterId = callInvitationData.inviter?.userID else {
            return
        }
        xLog("邀请者 id: \(inviterId)")
        
        let group = DispatchGroup()
        let queue = DispatchQueue(label: "inviterGCD", attributes: .concurrent)
        
        var inviterUserModel: UserModel?
        var myUserModel: UserModel?
        
        group.enter()
        queue.async(group: group) {
            // 获取邀请者信息
            MineMainViewModel.loadUserInfo(inviterId) { userModel in
                inviterUserModel = userModel
                group.leave()
            }
        }
        
        group.enter()
        queue.async(group: group) {
            MineMainViewModel.loadUserInfo(String(UserProfile.userId)) { userModel in
                myUserModel = userModel
                group.leave()
            }
        }
        
        // 当所有任务完成时通知
        group.notify(queue: DispatchQueue.main) { [weak self] in
            xLog("所有任务完成")
            /**
             接听
             
             1.接听者是中国用户,拨打者是海外用户,接听者不是翻译
             接听者积分 <= 0  不能接听,提示积分充值 ✅
             接听者积分 > 0 可以接听 ✅
             
             2.接听者是中国用户,拨打者是海外用户,接听者是翻译
             可以接听
             
             3.接听者是中国用户,拨打者是中国用户
             接听者积分 <= 0  不能接听,提示积分充值
             接听者积分 > 0 可以接听
             
             */
            
            if inviterUserModel?.country == 0 {
                // 我是中国人, 中国给中国打, 接不拦
                self?.receivedInvitation()
                return
            }
            
            // 我是翻译, 接不拦
            if IdentityType(rawValue: UserProfile.identityId) == .interpreter {
                self?.receivedInvitation()
                return
            }
            
            // 我不是翻译
            if (myUserModel?.score ?? 0) > 0 {
                // 积分充足,可以接听
                self?.receivedInvitation()
                
            } else {
                // 没有积分,不许接听,去充值页面, 拒绝通话
                self?.refuseInvitation()
                
                self?.dismiss(animated: false) {
                    let url = SystemEnvManager.shared.forwarderUrl + "/pages/vip/scorePoint?" + H5UrlManager.urlParameter()
                    let vc = BaseWebViewController(url: url, isNavBarHide: true)
                    // 只有中国人会收到这个提示
                    vc.message = "当前积分不足，无法接起国际通话。"
                    currentController?.push(vc)
                }
            }
        }
    }
    
    @objc func onRefuseInvitationAction() {
        refuseInvitation()
    }
    
    // 接受邀请
    func receivedInvitation() {
        ZegoCallAudioPlayerTool.stopPlay()
        guard let invitationData = callInvitationData else { return }
        dismiss(animated: false, completion: {
            var normalConfig = ZegoUIKitPrebuiltCallConfig.oneOnOneVoiceCall()
            if invitationData.invitees?.count ?? 0 > 1 {
                normalConfig = invitationData.type == .videoCall ? ZegoUIKitPrebuiltCallConfig.groupVideoCall() : ZegoUIKitPrebuiltCallConfig.groupVoiceCall()
            } else {
                //one on one call
                normalConfig = invitationData.type == .videoCall ? ZegoUIKitPrebuiltCallConfig.oneOnOneVideoCall() : ZegoUIKitPrebuiltCallConfig.oneOnOneVoiceCall()
            }
            let config = ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.requireConfig(invitationData) ?? normalConfig
            let callVC: ZegoUIKitPrebuiltCallVC = ZegoUIKitPrebuiltCallVC.init(invitationData, config: config)
            callVC.modalPresentationStyle = .fullScreen
            callVC.delegate = ZegoUIKitPrebuiltCallInvitationService.shared.help
            currentViewController()?.present(callVC, animated: true, completion: nil)
            ZegoCallInvitationDialog.hide()
            ZegoUIKitPrebuiltCallInvitationService.shared.callVC = callVC
            ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.onIncomingCallAcceptButtonPressed?()
            ZegoCallAudioPlayerTool.stopPlay()
            ZegoUIKitPrebuiltCallInvitationService.shared.state = .accept
            //        ZegoUIKitPrebuiltCallInvitationService.shared.invitationData = nil
            ZegoUIKitSignalingPluginImpl.shared.acceptInvitation(invitationData.inviter?.userID ?? "", data: nil, callback: nil)
        })
    }
    
    /// 拒绝邀请
    public func refuseInvitation() {
        
        ZegoCallAudioPlayerTool.stopPlay()
        ZegoUIKitPrebuiltCallInvitationService.shared.delegate?.onIncomingCallDeclineButtonPressed?()
        ZegoUIKitPrebuiltCallInvitationService.shared.invitationData = nil
        ZegoUIKitPrebuiltCallInvitationService.shared.callID = nil
        ZegoCallInvitationDialog.hide()
        ZegoUIKitPrebuiltCallInvitationService.shared.state = .normal
        
        guard let callInvitationData = callInvitationData else { return }
        
        ZegoUIKitSignalingPluginImpl.shared.refuseInvitation(callInvitationData.inviter?.userID ?? "", data: [
            "reason": "decline" as AnyObject,
            "invitationID": callInvitationData.invitationID as AnyObject
        ].jsonString)
    }
}
